package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.MemberLevelDAO;
import com.fly.estore.domain.MemberLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/12.
 */
@Repository("MemberLevelDAO")
public class MemberLevelDAOImpl extends BaseDAO<MemberLevel> implements MemberLevelDAO {

    public int addMemberLevel(final MemberLevel memberLevel) {
        StringBuilder sql = new StringBuilder("insert into member_level(member_level_id, level_name, level_icon, discount, create_id, create_time)");
        sql.append("values(?, ?, ?, ?, ?, ?)");
        int update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, memberLevel.getMemberLevelId());
                ps.setString(i++, memberLevel.getLevelName());
                ps.setString(i++, memberLevel.getLevelIcon());
                ps.setFloat(i++, memberLevel.getDiscount());
                ps.setString(i++, memberLevel.getCreateId());
                ps.setString(i++, memberLevel.getCreateTime());
            }
        });
        return update;
    }

    public int updateMemberLevel(final MemberLevel memberLevel) {
        StringBuilder sql = new StringBuilder();
        sql.append("update member_level set ")
                .append(" level_name = ?,")
                .append(" level_icon = ?,")
                .append(" discount = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where member_level_id = ?");
        int update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, memberLevel.getLevelName());
                ps.setString(i++, memberLevel.getLevelIcon());
                ps.setFloat(i++, memberLevel.getDiscount());
                ps.setString(i++, memberLevel.getLastUpdateId());
                ps.setString(i++, memberLevel.getLastUpdateTime());
                ps.setString(i++, memberLevel.getMemberLevelId());
            }
        });
        return update;
    }

    public int deleteById(final String memberLevelId) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from member_level where member_level_id = ?");
        int update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, memberLevelId);
            }
        });
        return update;
    }

    public MemberLevel getById(String memberLevelId) {
        MemberLevel memberLevel = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select * from member_level where member_level_id = ? order by create_time desc");
        List<MemberLevel> resultList = getJdbcTemplate().query(sql.toString(), new MemberLevelRowMapper(), new Object[]{memberLevelId});
        if (!CollectionUtils.isEmpty(resultList)){
            memberLevel = resultList.get(0);
        }
        return memberLevel;
    }

    public List<MemberLevel> getMemberLevelList() {
        List<MemberLevel> memberLevelList = new ArrayList<MemberLevel>();
        StringBuilder sql = new StringBuilder();
        sql.append("select * from member_level order by create_time desc");
        memberLevelList = getJdbcTemplate().query(sql.toString(), new MemberLevelRowMapper());
        return memberLevelList;
    }

    public class MemberLevelRowMapper implements RowMapper<MemberLevel> {
        public MemberLevel mapRow(ResultSet rs, int rowNum) throws SQLException {
            MemberLevel memberLevel = new MemberLevel();
            memberLevel.setMemberLevelId(rs.getString("member_level_id"));
            memberLevel.setLevelName(rs.getString("level_name"));
            memberLevel.setLevelIcon(rs.getString("level_icon"));
            memberLevel.setDiscount(rs.getFloat("discount"));
            memberLevel.setCreateId(rs.getString("create_id"));
            memberLevel.setCreateTime(rs.getString("create_time"));
            memberLevel.setLastUpdateId(rs.getString("last_update_id"));
            memberLevel.setLastUpdateTime(rs.getString("last_update_time"));
            return memberLevel;
        }
    }
}
